package org.luxor.common.netty.jdk.bio.client;

import org.luxor.commons.core.utils.IoUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Java原生：阻塞I/O案例 客户端
 *
 * @author Mr.yan @date 2023/8/29
 */
public class TimeClient {

    public static void main(String[] args) throws IOException {
        int port = 8080;
        if (args != null && args.length > 0) {
            port = Integer.valueOf(args[0]);
        }

        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            socket = new Socket("127.0.0.1", port);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);

            // 往服务端写入指令(QUERY TIME ORDER)
            out.println("QUERY TIME ORDER");
            out.flush();
            System.out.println("Send order 2 server succeed.");

            // 从服务端的读取响应
            String resp = in.readLine();
            System.out.println("Now is : " + resp);
        } finally {
            IoUtils.closeQuietly(out);
            IoUtils.closeQuietly(in);
            IoUtils.closeQuietly(socket);
        }
    }
}
